package com.junzhiit.nengji.app.rabbitmq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class RabbitmqEnum {
	
	public enum Queues {
		/**
		 * 根据配置文件的spring.rabbitmq.queues值的顺序下标自动封装队列名进name属性
		 */
		SHOP_BUSINESS_RECORD(0),
		SHOP_UPDATE(1);
		
		private String name;
		
		private Queues(int index) {
			this.name = queues.get(index);
		}
		
		public String getName() {
			return name;
		}
		
		public static Queues getQueue(String name) {
			for (Queues queue : values()) {
				if(queue.getName().equals(name)) {
					return queue;
				}
			}
			return null;
		}
		
		public static String[] getQueues() {
			Queues[] queues = values();
			int length = queues.length;
			String[] queueNames = new String[length];
			for (int i = 0; i < length; i++) {
				queueNames[i] = queues[i].getName();
			}
			return queueNames;
		}
	}
	
	
	private static Map<Integer, String> queues = new HashMap<>();
	
	@Value("${application.profile}")
	private String profile;
	
	@Value("${spring.rabbitmq.queues}")
	public void setQueues(String queues) {
		profile = profile == null ? "" : profile.trim() +"-";
		if(profile.startsWith("prod")) {
			profile = "";
		}
		String[] queueNames = queues.split(",");
		for (int i = 0; i < queueNames.length; i++) {
			String queue = profile + queueNames[i].trim();
			RabbitmqEnum.queues.put(i, queue);
		}
	}
}
